﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http;
using Ewk.MachineLearning.Classification.Conversions.EntityVectorFactories;
using Ewk.MachineLearning.Clustering.MultidimensionalScaling;
using Ewk.MachineLearning.Persistance.Domain;

namespace Clustering.MultidimensionalScaling.Web.UI.Controllers
{
    public abstract class DistancesController<T> : ApiController
        where T : Entity
    {
        public async Task<IEnumerable<EntityDifferences<double>>> GetAsync()
        {
            return await Task.Factory.StartNew(() =>
                {
                    var differenceContainer = GetEntityDifferenceContainer();

                    return differenceContainer.EntityDifferences;
                });
        }

        public async Task<EntityDifferences<double>> GetAsync(Guid id)
        {
            return await Task.Factory.StartNew(() =>
                {
                    var differenceContainer = GetEntityDifferenceContainer();

                    return differenceContainer.EntityDifferences
                                              .Single(differences => differences.EntityId == id.ToString());
                });
        }

        private static EntityDifferenceContainer<double> GetEntityDifferenceContainer()
        {
            var entityType = typeof(T);

            var vectors = AbstractEntityVectorFactory.GetEntityVectorFactory(entityType)
                                                     .CreateEntityVectors();

            return new EntityAngleContainer(entityType, vectors);
            // return new EntityDistanceContainer(entityType, vectors);
        }
    }
}